//
//  UIImage+clipscreen.m
//  bitmap_image
//
//  Created by mac on 15-7-12.
//  Copyright (c) 2015年 duanan. All rights reserved.
//

#import "UIImage+clipscreen.h"

@implementation UIImage (clipscreen)
+ (instancetype)clipscreenWithView:(UIView *)view savename:(NSString*)name
{
    // 1.开启上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    // 2.将控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.结束上下文
    UIGraphicsEndImageContext();
    
    if (name!=nil )
    {
        // 5.将image对象压缩为PNG格式的二进制数据
        NSData *data = UIImagePNGRepresentation(newImage);
        //    UIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)
        // 6.获取文件路径
        
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",name]];
        //保存图片
        [data writeToFile:path atomically:YES];
    }
    return newImage;
}

@end
